﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Sistema_Test_OnLine.Persistence
{
    public class AccessDB
    {
        private MySqlConnection con;
        private MySqlDataReader myReader;

        public AccessDB() 
        { 
            con = null;
        }

        public void Connect()
        {
            String myConnectionString = "server=localhost;User Id=root;database=test;password=";
            con = new MySqlConnection(myConnectionString);
            con.Open();
        }

        public void Disconnect()
        {
            con.Close();
        }

        public void Update(string sql)
        {
            MySqlCommand myCommand = new MySqlCommand(sql);
            myCommand.Connection = con;
            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
        }

        public void Find(string table, string condition)
        {
            string sql = "select * from " + table;
            if (!condition.Equals(""))
                sql = sql + " Where " + condition;
            MySqlCommand myCommand = new MySqlCommand(sql, con);
            myReader = myCommand.ExecuteReader();

        }

        public void add(string table, string condition)
        {
            string sql = "INSERT INTO " + table + " (cc, login, password, correo, nombre, apellido, nacimiento, sexo, rol) VALUES (" + condition + ")";
            MySqlCommand myCommand = new MySqlCommand(sql, con);
            myReader = myCommand.ExecuteReader();
        }

        public void update(string table, string condition1, string condition2)
        {
            string sql = "UPDATE "+table+" SET "+ condition1 + "WHERE " + condition2;
            MySqlCommand myCommand = new MySqlCommand(sql, con);
            myReader = myCommand.ExecuteReader();
            
        }

        public bool Next()
        {
            return myReader.Read();
        }

        public string GetString(string str)
        {
            return myReader.GetString(str);
        }

        public int GetInt(string str)
        {
            return myReader.GetInt32(str);
        }
        public MySqlDataReader GetmyReader()
        {
            return myReader;
        }
    }
}